//*************************************************************************************
// EGTB Validator Module
//*************************************************************************************

#include "stdafx.h"

#include "Validator.h"

namespace EndGameDB
{
	// progress function
	void (*OnValidationSubDbStartedPtr)(int wm, int wk, int bm, int bk, int wmrank, int bmrank);

	// validation error counter
	long validationWDL_Errors = 0;
	long validationDTM_Errors = 0;

	// maximum reached DTM
	int maxDTM = 0;

	// total counter of all positions
	long validationPositions = 0;

	// set default values
	void ClearResultValues(int& result, int& dtm)
	{
		result = LOSS;
		dtm = MAX_DTM;
	}


	// compares the results, if they are better, sets the results
	bool CompareResults(int& curResult, int& curDtm, int opponentResult, int opponentDtm)
	{
		// just check, if not UNKNOWN value
		if(opponentResult == UNKNOWN)
			return false;

		// switch according to the current result
		switch(curResult)
		{
		case LOSS:
			// if opponent is LOSS, we have WIN
			if(opponentResult == LOSS)
			{
				curResult = WIN;
				curDtm = opponentDtm;
				return true;
			}
			// DRAW is better position than LOSS
			else if(opponentResult == DRAW)
			{
				curResult = DRAW;
				curDtm = 0;
				return true;
			}
			else 
			{
				// better is LOSS, that lasts in the longer DTM
				if(opponentDtm > curDtm || curDtm == MAX_DTM)
				{
					curDtm = opponentDtm;
					return true;
				}
			}
			break;
		case DRAW:
			// if opponent is LOSS, we have WIN
			if(opponentResult == LOSS)
			{
				curResult = WIN;
				curDtm = opponentDtm;
				return true;
			}
			break;
		case WIN:
			// better only if smaller DTM
			if(opponentResult == LOSS)
			{
				if(opponentDtm < curDtm)
				{
					curDtm = opponentDtm;
					return true;
				}
			}
			break;
		}

		return false;
	}

}

